$(document).ready(function() {
	if (g_cfg['wiki_style_page']) {
		initChangeStyle();
	} else {
		initEditInPlace();
		loadHistory();
	}
	initHoverOver();
	initTabs();
});

function loadHistory() {
	var ajax_history_url = g_cfg['url_root']+g_cfg['history']+g_cfg['wiki_word'];
	$.get(ajax_history_url,{},function(data) {
		$("div#wiki_history").html(data);
		$("a.view","div#wiki_history").click(function(e) {
			e.preventDefault();
			$("td#view_panel").load($(this).attr("href"));
		});
		$("a.diff","div#wiki_history").click(function(e) {
			e.preventDefault();
			diff_version_url = $(this).attr("href");
			first_li = $("td#history_list ol li").filter(":first");
			current_version_url = $("a.diff",first_li).attr("href");
			$.get(current_version_url,{},function(s1) {
				$.get(diff_version_url,{},function(s2) {
					$("td#view_panel").html(StringDiff(s1,s2));
				});
			});
		});
		$("a.revert","div#wiki_history").click(function(e) {
			e.preventDefault();
			$("div#wiki_content").load($(this).attr("href"),{},function() {
				loadHistory();
			});
		});
	});

}

function initEditInPlace() {
	var edit_content_url  	= g_cfg['url_root']+g_cfg['create_wiki']+g_cfg['wiki_word'];
	var fetch_content_url   = g_cfg['url_root']+g_cfg['fetch_wiki']+g_cfg['wiki_word'];
	var edit_tags_url     	= g_cfg['url_root']+g_cfg['create_tags']+g_cfg['wiki_word'];
	var fetch_tags_url     	= g_cfg['url_root']+g_cfg['fetch_tags']+g_cfg['wiki_word'];
	var author_link		= g_cfg['url_root']+'wiki/author/';
	var tag_link		= g_cfg['url_root']+'wiki/view/tag:';
	
	if (edit_content_url != null) {
		$("div#wiki_content.editable").editable(edit_content_url, { 
        		type     : 'textarea',
			postload : fetch_content_url,
			value    : 'data',
			height   : '400px',
			width	 : '98%',
	        	submit   : 'OK',
			cancel	 : 'Cancel',
			onblur	 : 'ignore',
			event    : 'dblclick',
			callback : function(wiki){ 
					wiki = eval('(' +wiki+ ')'); 
					$("div#wiki_author a").html(wiki.author);
					$("div#wiki_author a").attr("href",author_link+wiki.author);
					$("div#wiki_creation a").html(wiki.created_on);

					return wiki.content; 
				   },
			rehover	: function(data){
					initHoverOver();
					}
			
		});
	}

	if (edit_tags_url != null) {
		$("div#wiki_tags.editable").editable(edit_tags_url, { 
        		type     : 'text',
			postload : fetch_tags_url,
			value    : 'data',
			height   : 'auto',
			width	 : '300px',
	        	submit   : 'OK',
			cancel	 : 'Cancel',
			onblur	 : 'ignore',
			event    : 'dblclick',
			callback : function(data){ 
					data = eval('(' +data + ')');
					var tag_links = "";
					for (var i = 0; i < data.length; i++) {
						tag_links += " <a href='"+tag_link+data[i].tag+"'>"+data[i].tag+"</a>";
					}
					return "Tags: " + tag_links; 
				   },
			rehover	: function(data){
					initHoverOver();
					}
			
		});
	}
}

function initHoverOver() {
	var ajax_spinner  = g_cfg['url_root']+g_cfg['ajax_spinner'];
	var ajax_fetch_url = g_cfg['url_root']+g_cfg['fetch_wiki'];
	
	$('div#tooltip').hide();
	
	$("a.wiki_word").hover(
		function() {
			var pos = $.extend($.iUtil.getPosition(this),$.iUtil.getSize(this));
			$("div#tooltip").html("<img src='"+ajax_spinner+"' />").css({
				top: pos.y + pos.h + 'px',
				left: pos.x + 'px'
			}).show();
			$("div#tooltip").load(ajax_fetch_url+$(this).html()+"/marked/hover");
		},
		function() {
			$("div#tooltip").hide();
		}
	);
}

function initTabs() {
	if (!$(".editable").length) {
		$("#wiki_history").remove();
		$("#wiki_tags").remove();
	}
	$("#wiki").tabs();
	
}

function initChangeStyle() {
	$("ul","#wiki_content").css("list-style","none");
	$("li","#wiki_content").each(function () {
		var current_li = $(this);
		var radio = document.createElement('input');
		$(radio).attr("type","radio");
		$(radio).attr("name","style");
		$(radio).attr("value",$("a",current_li).html());
		$(radio).click(function() {
			changeStyle($(this).attr("value"));
		});
		current_li.prepend(radio);
	});
}

function changeStyle(style_wiki) {
	var fetch_content_url   = g_cfg['url_root']+g_cfg['fetch_wiki']+style_wiki;
	var set_css_url		= g_cfg['url_root']+g_cfg['set_style']+style_wiki;
	$($("style").get(1)).load(fetch_content_url);
	$.post(set_css_url);
	
}
